package com.wonderful.eyepetizer.ui.pages.discover

import android.net.Uri
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.app.ktx.loadMoreView
import com.wonderful.eyepetizer.data.entity.bean.NewsItem
import com.wonderful.eyepetizer.ui.pages.detail.NewsDetailActivity
import com.wonderful.eyepetizer.ui.theme.News_bg

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/23 10:42
 */
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun NewsPage(viewModel: DiscoverViewModel = viewModel()) {
    val lazyPagingItems = viewModel.newsPageFlow.collectAsLazyPagingItems()
    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.newsRefreshing.value,
        onRefresh = { lazyPagingItems.refresh() })

    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
    ) {
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(top = 10.dp),
            content = {
                items(lazyPagingItems.itemCount) {
                    val newsItem = lazyPagingItems[it]
                    NewsItemWidget(newsItem = newsItem!!)
                }
                loadMoreView(lazyPagingItems)
            })
        PullRefreshIndicator(
            refreshing = viewModel.newsRefreshing.value,
            state = pullRefreshState,
            modifier = Modifier.align(Alignment.TopCenter)
        )
    }
}

@Composable
fun NewsItemWidget(newsItem: NewsItem) {
    val context = LocalContext.current
    if (newsItem.type == "textCard") {
        Text(
            text = newsItem.data.text,
            style = TextStyle(fontSize = 22.sp, fontWeight = FontWeight.Bold, color = Color.Black),
            modifier = Modifier.padding(start = 10.dp, top = 5.dp)
        )
    } else {
        Card(
            backgroundColor = News_bg,
            shape = RoundedCornerShape(4.dp),
            modifier = Modifier
                .padding(10.dp)
                .clickable {
                    val actionUrl = newsItem.data.actionUrl
                    val title = newsItem.data.titleList?.get(0) ?: ""
                    var webUrl = Uri.decode(actionUrl.substring(actionUrl.indexOf("url")))
                    webUrl = webUrl.substring(4)
                    NewsDetailActivity.actionStart(context, webUrl, title)
                }
        ) {
            Column {
                Image(
                    painter = rememberAsyncImagePainter(model = newsItem.data.backgroundImage),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(140.dp),
                    contentScale = ContentScale.FillWidth
                )
                newsItem.data.titleList?.run {
                    this.forEach {
                        Text(
                            text = it,
                            fontSize = 12.sp,
                            maxLines = 3,
                            overflow = TextOverflow.Ellipsis,
                            modifier = Modifier.padding(horizontal = 10.dp, vertical = 5.dp)
                        )
                    }
                }
            }
        }
    }
}